﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DONET101_Array
{
    class Program
    {
        static void PrintArray(string[] array)
        {
            for (int i = 0; i < array.Length; i++)
                array[i] = "Monday";
        }

        static void Main(string[] args)
        {
            // Declare and initialize an array:
            int[,] theArray = new int[5, 10];
            System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank);

            int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
            foreach (int i in numbers2D)
            {
                System.Console.Write("{0} ", i);
            }

            // Declare and initialize an array:
            string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

            // Pass the array as a parameter:
            PrintArray(weekDays);

            foreach (string s in weekDays)
                Console.WriteLine(s);

            TestOut.TestArrayOut();

            TestRef.TestArrayRef();

            TestImplicitlyTypedArray.TestVar();

            TestImplicitlyTypedLocals2.TestVar2();

            Console.WriteLine("Bye");
        }
    }

    class TestOut
    {
        static void FillArray(out int[] arr)
        {
            // Initialize the array:
            arr = new int[5] { 1, 2, 3, 4, 5 };
        }

        public static void TestArrayOut()
        {
            int[] theArray; // Initialization is not required

            // Pass the array to the callee using out:
            FillArray(out theArray);

            // Display the array elements:
            System.Console.WriteLine("Array elements are:");
            for (int i = 0; i < theArray.Length; i++)
            {
                System.Console.Write(theArray[i] + " ");
            }
        }
    }

    class TestRef
    {
        static void FillArray(ref int[] arr)
        {
            // Create the array on demand:
            if (arr == null)
            {
                arr = new int[10];
            }
            // Fill the array:
            arr[0] = 1111;
            arr[4] = 5555;
        }

        public static void TestArrayRef()
        {
            // Initialize the array:
            int[] theArray = { 1, 2, 3, 4, 5 };

            // Pass the array using ref:
            FillArray(ref theArray);

            // Display the updated array:
            System.Console.WriteLine("\nArray elements are:");
            for (int i = 0; i < theArray.Length; i++)
            {
                System.Console.Write(theArray[i] + " ");
            }
        }
    }

    class TestImplicitlyTypedArray
    {
        public static void TestVar()
        {
            var a = new[] { 1, 10, 100, 1000 }; // int[]
            var b = new[] { "hello", null, "world" }; // string[]

            // single-dimension jagged array
            var c = new[]   
            {  
                new[]{1,2,3,4},
                new[]{5,6,7,8}
            };

            // jagged array of strings
            var d = new[]   
            {
                new[]{"Luca", "Mads", "Luke", "Dinesh"},
                new[]{"Karen", "Suma", "Frances"}
            };

            var contacts = new[] 
            {
                new {
                        Name = " Eugene Zabokritski",
                        PhoneNumbers = new[] { "206-555-0108", "425-555-0001" }
                    },
                new {
                        Name = " Hanying Feng",
                        PhoneNumbers = new[] { "650-555-0199" }
                    }
            };

            foreach (int i in a)
            {
                Console.WriteLine(i);
            }

            foreach (var o in contacts)
            {
                Console.WriteLine(o.Name);
                foreach (string st in o.PhoneNumbers)
                    Console.WriteLine(st);
            }
        }
    }

    class TestImplicitlyTypedLocals2
    {
        public static void TestVar2()
        {
            string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };

            // If a query produces a sequence of anonymous types, 
            // then you must also use var in the foreach statement.
            var upperLowerWords =
                 from w in words
                 select new { Upper = w.ToUpper(), Lower = w.ToLower() };

            // Execute the query
            foreach (var ul in upperLowerWords)
            {
                Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
            }
        }
    }
    /* Outputs:
        Uppercase: APPLE, Lowercase: apple
        Uppercase: BLUEBERRY, Lowercase: blueberry
        Uppercase: CHERRY, Lowercase: cherry        
     */
}
